package com.hongye.house.service.impl;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.Query;

import com.hongye.house.dao.HouseImgDao;
import com.hongye.house.entity.HouseImgEntity;
import com.hongye.house.service.HouseImgService;


@Service("houseImgService")
public class HouseImgServiceImpl extends ServiceImpl<HouseImgDao, HouseImgEntity> implements HouseImgService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HouseImgEntity> page = this.page(
                new Query<HouseImgEntity>().getPage(params),
                new QueryWrapper<HouseImgEntity>()
        );

        return new PageUtils(page);
    }

   @Scheduled(cron = "0 10 3 ? * 1")
    public void jsup(){
        System.out.println("开始");
        HouseImgEntity houseImgEntity = new HouseImgEntity();
        String url = "https://sh.fang.lianjia.com/loupan/p_bgyxzbkpgr/huxingtu/";

        Document document = null;
        try {
            document = Jsoup.connect(url).get();

        } catch (IOException e) {
            e.printStackTrace();
        }

        Elements elementsByClass = document.getElementsByClass("item-list clear");
        //List<HouseImgEntity> houseImgEntities = baseMapper.selectList(null);
        Elements img = elementsByClass.select("img");
        img.forEach(trim->{
            String src  =trim.attr("src");
            System.out.println(src);
            HouseImgEntity homeImg = baseMapper.selectOne(new QueryWrapper<HouseImgEntity>().eq("home_img", src));
            if (homeImg!=null){
                System.out.println("当前数据已存在"+homeImg);
                return;
            }else if (homeImg==null){
                houseImgEntity.setHomeShapeImg(src);
                houseImgEntity.setHouseId(null);
                baseMapper.insert(houseImgEntity);
            }

        });


    }
}